#! python
# -*- coding: utf-8 -*-

import time
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def get_token(filepath="gitee"):
    """获取Gitee的Token"""
    if not os.path.exists(filepath):
        home = os.getenv('UserProfile')
        if home is None:
            home = os.getenv('HOME')
        filepath = f'{home}/.config/gitee'
    with open(filepath, encoding='UTF-8') as fd:
        line = fd.readline()
    return line


def get_browser(options=None):
    """return browser"""
    if options is None:
        options = Options()
    driver = webdriver.Edge(options=options)
    return driver


driver = get_browser()
navigation_bar = '//div[@id="git-discover-page"]/child::a'
next_selector = '[@class="icon item" and @rel="next"]'
timeout = 20

def find_next_link(driver=driver, selector=next_selector, parent=navigation_bar, timeout=timeout):
    """ find link to next page """
    xpath = parent + selector
    locator = (By.XPATH, xpath)
    condition = EC.presence_of_all_elements_located(locator)
    links = WebDriverWait(driver, timeout).until(condition)
    if len(links) == 0:
        return None
    return links[0]


projects_container = '//ul[@id="search-projects-ulist"]/descendant::a'
project_selector = '[@class="repository"]'

def keyboard():
    """like function `keyboard' in matlab """
    import pdb
    pdb.set_trace()


def get_repositories(links):
    """ return repos of link items """
    repos = []
    for element in links:
        href = element.get_property('href')
        repos.append(href)
    return repos


def get_project_items(driver=driver,
                     selector=project_selector,
                     parent=projects_container,
                     timeout=timeout):
    """ get links for projects in container """
    try:
        xpath = parent + selector
        locator = (By.XPATH, xpath)
        condition = EC.presence_of_all_elements_located(locator)
        links = WebDriverWait(driver, timeout).until(condition)
    except:
        links = driver.find_elements(*locator)
    return links


def goto_next_page(driver=driver, 
                   selector=next_selector, 
                   parent=navigation_bar, 
                   timeout=timeout,
                   **kwargs):
    """ goto next page """
    active_page_selector = '[@class="item active"]'
    active_page_link = find_next_link(driver, selector=active_page_selector, parent=navigation_bar, timeout=timeout)
    active_index = active_page_link.text
    # print(f'before click: current page {active_index}')
    try:
<<<<<<< HEAD:spider.py
        xpath = parent + selector
        locator = (By.XPATH, xpath)
        condition = EC.element_to_be_clickable(locator)
        wait = WebDriverWait(driver, timeout)
        link = wait.until(condition)
        link.click()
        wait.until(EC.presence_of_all_elements_located((By.XPATH, '//*')))
        # driver.refresh()
        active_page_link = find_next_link(driver, selector=active_page_selector, parent=navigation_bar, timeout=timeout)
        active_index = active_page_link.text
        # print(f'after click: current page {active_index}')
        return wait.until(condition)
=======
        print("goto_next_page")
        link = find_page(driver, selector, parent)
        if link is not None:
            print(f'link to {link.get_property("href")}')
            link.click()
            locator = (By.XPATH, '//*')
            condition = EC.presence_of_all_elements_located(locator)
            return WebDriverWait(driver, timeout).until(condition)
>>>>>>> 94379f1 (  commit changes):notebook/.ipynb_checkpoints/spider-checkpoint.py
    except:
        return None


url = 'https://gitee.com/BIT_chenxinliang/projects'
def main(driver=driver, url=url, timeout=timeout):
    """ 登录 """
    driver.implicitly_wait(timeout)
    driver.get(url)
    # explicitly_wait(driver, timeout)
    repos = []
    next_page_link = find_next_link(driver=driver)
    while next_page_link is not None:
        links = get_project_items(driver=driver,
                                 selector=project_selector,
                                 parent=projects_container,
                                 timeout=timeout)
        repos = repos + get_repositories(links)
        next_page_link = goto_next_page(driver=driver, timeout=timeout)
        time.sleep(3)
    for p in repos:
        print(p)
    return repos


if __name__ == "__main__":
    main()
